const gulp = require('gulp');
const sass = require('gulp-sass');
const jsonImporter = require('node-sass-json-importer');
const yamlImporter = require('node-sass-yaml-importer');
const magicImporter = require('node-sass-magic-importer');
const sourcemaps = require('gulp-sourcemaps');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const selectornot = require("postcss-selector-not");
const cssImageSet = require('postcss-image-set-polyfill');
const quantityQueries = require('postcss-quantity-queries');
const clean = require('postcss-clean');
const cssnano = require('cssnano');
const replace = require('gulp-replace');
const path = require('path');
const fs = require('fs');
const babel = require('gulp-babel');
const mocha = require('gulp-mocha');
const minify = require('gulp-minify');
const config = require("./config");

const PAGE = config.build.page;

gulp.task('sass', function () {
    var plugins = [
        quantityQueries(),
        cssImageSet(),
        selectornot(),
        autoprefixer({
            browsers: ['IE >= 8', 'iOS 7', 'Firefox > 30']
        }),
        cssnano({
        }),
    ];
    gulp.src(`src/**/*.scss`)
        .pipe(sourcemaps.init())
        .pipe(sass({
            data: '',
            importer: [
                yamlImporter,
                jsonImporter,
                magicImporter(),
            ]
        }).on('error', sass.logError))
        .pipe(postcss(plugins))
        .pipe(replace('Symbol(GANG)', '\\'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('src/assets/css'))
});

gulp.task('sass:watch', function () {
    gulp.watch('src/**/*.scss', ['sass']);
});

gulp.task('test', function () {
    return gulp.src('test/module/*.js')
        .pipe(babel(
            {
                presets: ['env']
            }
        ))
        .pipe(gulp.dest("./test"))
});

gulp.task('compress', function() {
    gulp.src(`dist/static/js/${PAGE}/*.js`)
      .pipe(minify({
          ext:{
              src:'-debug.js',
              min:'.js'
          },
          exclude: ['tasks'],
          ignoreFiles: ['.combo.js', '-min.js']
      }))
      .pipe(gulp.dest(`dist/static/js/${PAGE}`))
  });